package gui.application;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import gui.url.HttpClientHelper;
public class PasswordApplication extends JFrame implements ActionListener{
	private static final long serialVersionUID = -1189035634361220261L;
    JFrame mainframe;
    JScrollPane panel;
    //创建相关的Label标签
    JLabel input_label = new JLabel("输入(http://198.120.10.10:8080):");
    //创建相关的文本域
    JScrollPane inputJscrollPane;
    JTextArea inputTextArea = new JTextArea();
    //创建滚动条以及输出文本域
    JScrollPane jscrollPane;
    JTextArea outtext_textarea = new JTextArea();
    //创建按钮
    JButton infilepath_button = new JButton("授权");
    JButton outfilepath_button = new JButton("取消授权");

    public void show(){
        mainframe = new JFrame("授权工具");
        // Setting the width and height of frame
        mainframe.setSize(575, 550);
        mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainframe.setResizable(false);//固定窗体大小

        Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包 
        Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸 
        int screenWidth = screenSize.width/2; // 获取屏幕的宽
        int screenHeight = screenSize.height/2; // 获取屏幕的高
        int height = mainframe.getHeight(); //获取窗口高度
        int width = mainframe.getWidth(); //获取窗口宽度
        mainframe.setLocation(screenWidth-width/2, screenHeight-height/2);//将窗口设置到屏幕的中部             
        //窗体居中，c是Component类的父窗口
        initPanel();//初始化面板
        mainframe.add(panel);
        mainframe.setVisible(true);
    }
     /* 创建面板，这个类似于 HTML 的 div 标签
     * 我们可以创建多个面板并在 JFrame 中指定位置
     * 面板中我们可以添加文本字段，按钮及其他组件。
     */
    public void initPanel(){
        this.panel = new JScrollPane();
        panel.setLayout(null);
        
        input_label.setBounds(10,20,200,25);
        inputTextArea.setFont(new Font("标楷体", Font.BOLD, 16));
        inputJscrollPane = new JScrollPane(inputTextArea);
        inputJscrollPane.setBounds(10,50, 550, 150);
        this.panel.add(input_label);
        this.panel.add(inputJscrollPane);
        
        infilepath_button.setBounds(10, 210, 120, 25);
        outfilepath_button.setBounds(150,210,120,25);
        this.panel.add(infilepath_button);
        this.panel.add(outfilepath_button);
        
//        output_label.setBounds(10,250,120,25);
//        outtext_textarea.setFont(new Font("标楷体", Font.BOLD, 16));
//        jscrollPane = new JScrollPane(outtext_textarea);
//        jscrollPane.setBounds(10,280, 550, 150);
//        this.panel.add(output_label);
//        this.panel.add(jscrollPane);
        //增加动作监听
        infilepath_button.addActionListener(this);
        outfilepath_button.addActionListener(this);
    }
    /**
     * 单击动作触发方法
     * @param event
     */
    public void actionPerformed(ActionEvent event) {
        // TODO Auto-generated method stub
        if(event.getSource() == infilepath_button){
//        	HttpClientHelper.sendPost(urlParam, params, charset)
        	String url = inputTextArea.getText();
        	String hash = getHash(url);
        	if(StringUtils.equals("error", hash)) {
        		JOptionPane.showMessageDialog(null, "失败", "提示", 2);
        	}else{
        		try {
					String license = ComputerIdentifier.getLicense(hash);
					boolean result = generate(url,license);
					if(result) {
						JOptionPane.showMessageDialog(null, "成功", "提示", 2);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
        	}
        }else if(event.getSource() == outfilepath_button){
        	String url = inputTextArea.getText();
        	boolean result = cancel(url);
        	if(result) {
        		JOptionPane.showMessageDialog(null, "成功", "提示", 2);
        	}else {
        		JOptionPane.showMessageDialog(null, "是吧", "提示", 2);
        	}
        }
    }
    public void windowClosed(WindowEvent arg0) {         
        System.exit(0);
    } 
    public void windowClosing(WindowEvent arg0) { 
        System.exit(0);
    }
    private String hash = "/api/license/hash";
    private String generate = "/api/license/generate";
    private String cancel = "/api/license/cancel";
    private String getHash(String domain) {
		String result = HttpClientHelper.doGet(domain+hash);//HttpClientHelper.sendPost(domain+url, null, "utf-8");
		JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
		if(!returnData.get("success").getAsBoolean()) {
			JOptionPane.showMessageDialog(null, "出现错误", "提示", 2);
			return "error";
		}
		return returnData.getAsJsonObject("result").get("item").getAsString();
    }
    private boolean generate(String domain,String hash) {
//    	Map<String,Object> params = new HashMap<String,Object>();
//    	params.put("license",hash);
    	JsonObject json = new JsonObject();
    	json.addProperty("license", hash);
	    	String result = HttpClientHelper.doPost2(domain+generate, json);
    	JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
    	return returnData.get("success").getAsBoolean();
    }
    private boolean cancel(String domain) {
    	JsonObject json = new JsonObject();
    	String result = HttpClientHelper.doPost2(domain+cancel, json);
    	JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
    	System.out.println("returnData is:"+returnData);
    	return returnData.get("success").getAsBoolean();
    }
}
